package com.welove.activity.login;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.welove.R;
import com.welove.activity.BaseActivity;
import com.welove.util.LogFactory;
import com.welove.util.ValueUtil;

/**
 * @author dreamfutureba@gmail.com
 * @date 2013-11-3下午8:40:24
 * @version
 * @description 注册界面
 */
public class RegisterActivity extends BaseActivity {
	private static final String TAG = RegisterActivity.class.getSimpleName();

	private ViewGroup activity_register;
	private RelativeLayout activity_register_phone;
	private RelativeLayout activity_register_email;
	private RadioGroup register_group;

	/** 邮箱注册界面控件 */
	private EditText register_et_email;
	private EditText register_et_nikename;
	private EditText register_et_password;
	private Button register_btn_confirm;
	private CheckBox register_check_remeber_me;
	private TextView register_rights_notice;
	
	private String email,nikename,password;
	
	/** 手机注册界面控件 */
	private LinearLayout register_phone_1step_layout;// 第一步
	private EditText register_phone_et_num;
	private LinearLayout register_phone_2step_layout;// 第二步
	private EditText register_phone_et_nikename;
	private EditText register_phone_et_password;
	private LinearLayout register_phone_3step_layout;// 第三步
	private EditText register_phone_et_verificode;
	private Button register_phone_btn_send_verificode;
	private Button register_phone_btn_confirm;// 下一步按钮

	/** 倒计时 */
	private final int COUNT_DOWN_START = 0x10000000;
	private final int COUNT_DOWN_END = 0x10000001;
	private String mBtnContent1 = "点击获取验证码";
	private String mBtnContent2 = "重新获取验证码";
	private String mBtnContentRunning = "%s秒后重新发送";
	private static int startSeconds = 60;
	private static int endSeconds = 1;
	private Timer timer;

	@Override
	protected void installViews() {
		setContentView(R.layout.activity_register);
		initView();
	}

	private void initView() {
		activity_register = (ViewGroup) this
				.findViewById(R.id.activity_register);
		activity_register_phone = initRelative(R.id.activity_register_phone);
		activity_register_email = initRelative(R.id.activity_register_email);
		activity_register
				.setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE);

		register_group = initRp(R.id.register_group);
		/** 邮箱注册界面实例化 */
		register_et_email=initEt(R.id.register_et_email);
		register_et_nikename=initEt(R.id.register_et_nikename);
		register_et_password=initEt(R.id.register_et_password);
		register_btn_confirm=initBtn(R.id.register_btn_confirm);
		register_check_remeber_me=initCb(R.id.register_check_remeber_me);
		register_rights_notice=initTv(R.id.register_rights_notice);
		
		/** 手机注册界面控件实例化 */
		register_phone_1step_layout = initLinear(R.id.register_phone_1step_layout);
		register_phone_2step_layout = initLinear(R.id.register_phone_2step_layout);
		register_phone_3step_layout = initLinear(R.id.register_phone_3step_layout);
		register_phone_et_num = initEt(R.id.register_phone_et_num);
		register_phone_et_nikename = initEt(R.id.register_phone_et_nikename);
		register_phone_et_password = initEt(R.id.register_phone_et_password);
		register_phone_et_verificode = initEt(R.id.register_phone_et_verificode);
		register_phone_btn_send_verificode = initBtn(R.id.register_phone_btn_send_verificode);
		register_phone_btn_confirm = initBtn(R.id.register_phone_btn_confirm);
		/** 默认进入邮箱注册界面 */
		showEmailRegister();
		showPhoneRegisterOneStep();
		setBtnText(mBtnContent1);
	}

	@Override
	protected void registerEvents() {
		register_group
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						if (checkedId == R.id.register_rb_email) {
							showEmailRegister();
						} else if (checkedId == R.id.register_rb_phone) {
							showPhoneRegister();
						}
					}
				});
		register_phone_btn_send_verificode
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						startCountDown(true);
						setBtnEnabled(false);
					}
				});
		//阅读服务协议
		register_rights_notice.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				goRightsNotice();
			}
		});
		
		register_btn_confirm.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				email=register_et_email.getText().toString();
				nikename=register_et_nikename.getText().toString();
				password=register_et_password.getText().toString();
				
				if (checkEmailData()) {
					LogFactory.e(TAG, "跳转到下一界面");
					/**跳转到完善资料界面*/
					goPerfectInfo();
				}
			}
		});
	}

	@Override
	protected void initTitleBar() {
		super.initTitleBar();
		mTitleBar.titlebar_tv_center.setText("注册");
	}

	// 第一步确定按钮事件监听
	OnClickListener oneStepListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			showPhoneRegisterTwoStep();
		}
	};
	// 第二步确定按钮事件监听
	OnClickListener twoStepListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			showPhoneRegisterThreeStep();
		}
	};
	// 第三步确定按钮事件监听
	OnClickListener threeStepListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
		}
	};

	/**
	 * 显示邮箱注册界面
	 */
	private void showEmailRegister() {
		activity_register_email.setVisibility(View.VISIBLE);
		activity_register_phone.setVisibility(View.GONE);
		activity_register_email.requestFocus();
	}

	/**
	 * 显示手机注册界面
	 */
	private void showPhoneRegister() {
		activity_register_phone.setVisibility(View.VISIBLE);
		activity_register_email.setVisibility(View.GONE);
		activity_register_phone.requestFocus();
	}

	/**
	 * 显示手机注册第一步界面
	 */
	private void showPhoneRegisterOneStep() {
		register_phone_1step_layout.setVisibility(View.VISIBLE);
		register_phone_2step_layout.setVisibility(View.GONE);
		register_phone_3step_layout.setVisibility(View.GONE);
		register_phone_btn_confirm.setText("下一步");
		register_phone_btn_confirm.setOnClickListener(oneStepListener);
	}

	/**
	 * 显示手机注册第二步界面
	 */
	private void showPhoneRegisterTwoStep() {
		register_phone_1step_layout.setVisibility(View.GONE);
		register_phone_2step_layout.setVisibility(View.VISIBLE);
		register_phone_3step_layout.setVisibility(View.GONE);
		register_phone_btn_confirm.setText("注册");
		register_phone_btn_confirm.setOnClickListener(twoStepListener);
	}

	/**
	 * 显示手机注册第三步界面
	 */
	private void showPhoneRegisterThreeStep() {
		register_phone_1step_layout.setVisibility(View.GONE);
		register_phone_2step_layout.setVisibility(View.GONE);
		register_phone_3step_layout.setVisibility(View.VISIBLE);
		register_phone_btn_confirm.setText("下一步");
		register_phone_btn_confirm.setOnClickListener(threeStepListener);
	}

	/**
	 * 开始倒计时
	 * 
	 * @param isStart
	 */
	public void startCountDown(boolean isStart) {
		if (isStart) {
			countDown();
		}
	}

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == COUNT_DOWN_START) {
				setBtnText(String.format(mBtnContentRunning, msg.arg1));
			} else if (msg.what == COUNT_DOWN_END) {
				resetBtnCase();
			}
		}
	};

	private void countDown() {
		timer = new Timer();
		timer.schedule(new TimerTask() {
			int i = startSeconds;

			@Override
			public void run() {
				Message msg = handler.obtainMessage();
				msg.what = COUNT_DOWN_START;
				if (i < endSeconds) {
					msg.what = COUNT_DOWN_END;
					timer.cancel();
				}
				msg.arg1 = i;
				i--;
				handler.sendMessage(msg);
			}
		}, 0, 1000);
	}

	public void resetBtnCase() {
		if (timer != null) {
			timer.cancel();
		}
		setBtnEnabled(true);
		setBtnText(mBtnContent2);
	}

	/**
	 * 按钮是否可点击
	 * 
	 * @param enabled
	 */
	public void setBtnEnabled(boolean enabled) {
		register_phone_btn_send_verificode.setEnabled(enabled);
	}

	/**
	 * 设置按钮文字
	 * 
	 * @param text
	 */
	public void setBtnText(CharSequence text) {
		register_phone_btn_send_verificode.setText(text);
	}

	private boolean checkEmailData() {
		if (ValueUtil.isStrEmpty(email)) {
			showToast(0, "Email不能为空");
			return false;
		}if (!isEmail(email)) {
			showToast(0, "请输入正确的Email地址");
			return false;
		}
		if (ValueUtil.isStrEmpty(nikename)) {
			showToast(0, "昵称不能为空");
			return false;
		}
		if (ValueUtil.isStrEmpty(password)) {
			showToast(0, "密码不能为空");
			return false;
		}
		return true;
	}
	
	private void goRightsNotice(){
		Intent noticeIntent=new Intent(RegisterActivity.this, RegisterRightsNoticeActivity.class);
		startActivity(noticeIntent);
	}
	
	private void goPerfectInfo(){
		Intent perfcetInfoIntent=new Intent(RegisterActivity.this, RegisterPerfectInfoActivity.class);
		startActivity(perfcetInfoIntent);
	}

}
